package org.jsbd.boss.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMonitorConfigDao;
import org.jsbd.boss.domian.MonitorConfigBean;
import org.jsbd.boss.service.IMonitorConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("monitorConfigService")
public class MonitorConfigServiceImpl implements IMonitorConfigService {

	private static final String MONITOR_CONFIG_MAP_KEY = "MONITOR_CONFIG_MAP_KEY";

	@Autowired
	private IMonitorConfigDao monitorConfigDao;

	@Autowired
	private RedisClient redisClient;

	@Override
	public void saveMonitorConfig(MonitorConfigBean monitorConfig) {
		monitorConfigDao.saveMonitorConfig(monitorConfig);
		redisClient.remove(MONITOR_CONFIG_MAP_KEY);
	}

	@Override
	public void updateMonitorConfig(MonitorConfigBean monitorConfig) {
		monitorConfigDao.updateMonitorConfig(monitorConfig);
		redisClient.remove(MONITOR_CONFIG_MAP_KEY);
	}

	@Override
	public void updateMonitorConfigItemValue(MonitorConfigBean monitorConfig) {
		monitorConfigDao.updateMonitorConfigItemValue(monitorConfig);
	}

	@Override
	public MonitorConfigBean getMonitorConfig(MonitorConfigBean monitorConfig) {
		return monitorConfigDao.getMonitorConfig(monitorConfig);
	}

	@Override
	public List<MonitorConfigBean> getMonitorConfigList(MonitorConfigBean monitorConfig) {
		return monitorConfigDao.getMonitorConfigList(monitorConfig);
	}

	@Override
	public Map<String, Object> getMonitorConfigListPage(DataGridModel page, MonitorConfigBean monitorConfig) {
		return monitorConfigDao.getMonitorConfigListPage(page, monitorConfig);
	}

	@Override
	public Integer getMonitorConfigCount(MonitorConfigBean monitorConfig) {
		return monitorConfigDao.getMonitorConfigCount(monitorConfig);
	}

	@Override
	public void deleteMonitorConfig(MonitorConfigBean monitorConfig) {
		monitorConfigDao.deleteMonitorConfig(monitorConfig);
		redisClient.remove(MONITOR_CONFIG_MAP_KEY);
	}

	@Override
	public Map<String, String> getAllMonitorConfigMap() {
		Map<String, String> dataMap = (Map<String, String>) redisClient.get(MONITOR_CONFIG_MAP_KEY);
		if (dataMap == null) {
			List<MonitorConfigBean> configList = monitorConfigDao.getMonitorConfigList(new MonitorConfigBean());
			if (configList == null || configList.isEmpty()) {
				dataMap = new HashMap<String, String>();
				redisClient.put(MONITOR_CONFIG_MAP_KEY, dataMap);
				return dataMap;
			}

			dataMap = new HashMap<String, String>(configList.size());
			for (MonitorConfigBean config : configList) {
				dataMap.put(config.getItemKey(), config.getItemValue());
			}
			redisClient.put(MONITOR_CONFIG_MAP_KEY, dataMap);
		}
		return dataMap;
	}

	@Override
	public boolean hasParam(String param) {
		MonitorConfigBean mc = new MonitorConfigBean();
		mc.setItemKey(param);
		MonitorConfigBean result = monitorConfigDao.getMonitorConfig(mc);
		if (null == result) {
			return false;
		}
		return true;
	}

}
